package com.tistory.iam1492.server;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import it.sauronsoftware.cron4j.Scheduler;

public class ServerMain {
	//private final static String TEST_NOON = "00 12 * * *";
	private final static String DAYLY_PATTERN = "0 0 * * *";
	private final static String WEEKLY_PATTERN = "0 0 * * 0";
	private final static String MONTHLY_PATTERN = "0 0 1 * *";
	//static String[] envs = {"PATH=/home/bin:/bin:/usr/local/jdk/bin:/usr/bin"};
	static File directory;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			
		}catch(ClassNotFoundException e){
			
		}
		
		
		// Creates a Scheduler instance.
		Calendar calendar = new GregorianCalendar();
		TimeZone timeZone = calendar.getTimeZone();
		System.out.println("Start...");
		Scheduler dailyUpdate = new Scheduler();
		dailyUpdate.setTimeZone(timeZone);
		// Schedule a once-a-minute task.
		dailyUpdate.schedule(DAYLY_PATTERN, new Runnable() {
			public void run() {
				try{
					System.out.println("run...TEST_NOON");
					Connection conn;
					Statement stmt = null;
					String jdbcUrl;
					String id;
					String pw;
					jdbcUrl = "jdbc:mysql://localhost:3306/iam1493";
					id = "iam1493";
					pw = "qjxms@0!0";
					conn = DriverManager.getConnection(jdbcUrl, id, pw);
					stmt = conn.createStatement();
					stmt.executeUpdate("update star_list SET daily_vote = 0;");		
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
		});
		// Starts the scheduler.
		dailyUpdate.start();
		
		Scheduler weeklyUpdate = new Scheduler();
		weeklyUpdate.setTimeZone(timeZone);
		// Schedule a once-a-minute task.
		weeklyUpdate.schedule(WEEKLY_PATTERN, new Runnable() {
			public void run() {
				try{
					System.out.println("run...TEST_PATTERN");
					Connection conn;
					Statement stmt = null;
					String jdbcUrl;
					String id;
					String pw;
					jdbcUrl = "jdbc:mysql://localhost:3306/iam1493";
					id = "iam1493";
					pw = "qjxms@0!0";
					conn = DriverManager.getConnection(jdbcUrl, id, pw);
					stmt = conn.createStatement();
					stmt.executeUpdate("update star_list SET weekly_vote = 0;");	
					
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
		});
		// Starts the scheduler.
		weeklyUpdate.start();
		
		
		Scheduler monthlyUpdate = new Scheduler();
		monthlyUpdate.setTimeZone(timeZone);
		// Schedule a once-a-minute task.
		monthlyUpdate.schedule(MONTHLY_PATTERN, new Runnable() {
			public void run() {
				try{
					System.out.println("run...TEST_PATTERN");
					Connection conn;
					Statement stmt = null;
					String jdbcUrl;
					String id;
					String pw;
					jdbcUrl = "jdbc:mysql://localhost:3306/iam1493";
					id = "iam1493";
					pw = "qjxms@0!0";
					conn = DriverManager.getConnection(jdbcUrl, id, pw);
					stmt = conn.createStatement();
					stmt.executeUpdate("update star_list SET monthly_vote = 0;");	
					
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
		});
		// Starts the scheduler.
		monthlyUpdate.start();

	}

}
